package yw.substart.example.controller;

import com.alibaba.fastjson.JSON;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import yw.substart.example.service.ConfigService;
import yw.substart.example.service.WebSocketEndpoint;
import yw.substart.example.util.Config;
import yw.substart.example.vo.ConfigEntity;
import yw.substart.example.vo.ParamsVo;

import java.io.IOException;
import java.util.Map;

/**
 * @author lwq 2018/5/21
 * @comment
 */
@RestController
public class ServiceController extends BaseController {

    private final Logger logger = LoggerFactory.getLogger(ServiceController.class);

    @RequestMapping("/service/ip")
    public JSON getIpAddr() {
        return success(getIp());
    }

    @Autowired
    private ConfigService configService;

    @RequestMapping(value = "/service/add", method = RequestMethod.POST)
    public JSON add(ConfigEntity entity) {
        configService.addConfig(entity);
        WebSocketEndpoint.sendMessageToAll("===> add: " + JSON.toJSONString(entity));
        return success(entity);
    }

    @RequestMapping("/service/query")
    public JSON queryAll(@RequestParam(required = false, value = "n") String name) {
        if (StringUtils.isEmpty(name)) {
            return success(configService.findAllConfig());
        } else {
            Map map = Config.configMap.get(name);
            if (map == null) {
                return error("没有找到配置");
            }
            return success(map);
        }
    }

    @RequestMapping("/service/delete")
    public JSON delete(@RequestBody ParamsVo paramsVo) {
        configService.delete(paramsVo);
        return success();
    }

    @RequestMapping("/service/build")
    public JSON build(ParamsVo paramsVo) {
        try {
            configService.build(paramsVo);
            WebSocketEndpoint.sendMessageToAll("successful");
            return success("编译成功!");
        } catch (Exception e) {
            Map map = Config.configMap.get(paramsVo.getProjectName());
            map.put(ConfigEntity.STATUS, ConfigEntity.Status.FAIL.getCode());
            Config.configMap.put(paramsVo.getProjectName(), map);
            Config.write();
            WebSocketEndpoint.sendMessageToAll("fail");
            logger.error(e.getMessage(), e);
            return error(e.getMessage());
        }
    }

    @RequestMapping("/service/tomcat")
    public JSON tomcat(@RequestParam String action, @RequestParam String projectName) {
        try {
            configService.startOrStop(action, projectName);
            return success("执行成功!");
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            return success(e.getMessage());
        }

    }

    @RequestMapping("/service/command")
    public JSON command(@RequestParam String command) {
        try {
            configService.command(command);
        } catch (IOException e) {
            logger.error(e.getMessage(), e);
            return error(e.getMessage());
        }
        return success();
    }


}
